﻿using System;
using System.Collections.Generic;
//using System.Linq; - Not useful
using System.Text;
using System.Threading.Tasks;
// System.IO is for using Path.*
using System.IO;

namespace ExtNET
{
    public class Extensions
    {
        /*
         * This Static Dictionary called extkeys registers all the custom extensions
         */
        static Dictionary<string, string> extkeys = new Dictionary<string, string>();


        /*
         * The Static Void method "Add" adds an extension to "extkeys"
         * Used in this way:
         * --------
         * Extensions.Add("extension");
         * --------
         */
        public static void Add(string extkey)
        {
            extkeys.Add(extkey, extkey);
        }


        /*
         * This Static Void method "Rem" removes an extension from "extkeys"
         * Used in this way:
         * --------
         * Extensions.Rem("extension");
         * --------
         */
        public static void Rem(string extkey)
        {
            extkeys.Remove(extkey);
        }


        /*
         * Static Void method "Chk" checks if the extension is registered on "extkeys"
         * Used in this way:
         * --------
         * bool chk = Extension.Chk("extension");
         * --------
         */
        public static bool Chk(string extkey)
        {
            if(extkeys.ContainsKey(extkey))
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /*
         * Get a value from the Dictionary
         * Used in this way:
         * --------
         * Extensions.Gdv("extension");
         * ----------
         */
        public static string Gdv(string extkey)
        {
            if (extkeys.ContainsKey(extkey))             
            {
                string tgv = "";
                extkeys.TryGetValue(extkey, out tgv);
                return tgv;
            }
            else
            {
                return "null";
            }
        }
    }
    public class Filter
    {
        // Do not use _term, use DelimExt
        static string _term;


        /*
         * String property "DelimExt" defines what is the separator of the custom extension.
         * Instead of test.custom.txt, you can do test-custom.txt, replacing the point with "-".
         * Used in this way:
         * --------
         * Filter.DelimExt = "-";
         * --------
         */
        public static string DelimExt
        {
            get
            {
                return _term;
            }
            set
            {
                _term = value;
            }
        } 


        /*
         * Bool "ChkValid" checks if the custom extension selected matches with the file custom extension.
         * Used in this way:
         * --------
         * bool isValid;
         * if(Filter.ChkValid(@"C:\fakepath\cus.tom.txt", "tom"))
         * {
         *     isValid = true;
         *     // Here goes your code for the file.
         * }
         * {
         *     isValid = false;
         *     // Here goes your code if its not valid.
         * }
         */
        public static bool ChkValid(string path, string cext)
        {
            if(Filter.DelimExt == "")
            {
                Filter.DelimExt = ".";
            }
            else
            {
                Filter.DelimExt = Filter.DelimExt;
            }

            if(path.EndsWith(Filter.DelimExt + Extensions.Gdv(cext)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
